/*
 * gulp 全局安装     ====>npm install --global gulp
 * 项目的开发依赖安装====>npm install --save-dev gulp
 * 文件合并          ====>npm install gulp-concat --save-dev
 * 压缩html          ====>npm install gulp-htmlmin --save-dev
 * 压缩css           ====>npm install gulp-minify-css --save-dev
 * 压缩js            ====>npm install gulp-uglify --save-dev
 * 检查js            ====>npm install gulp-jshint --save-dev
 * 压缩图片          ====>npm install gulp-imagemin --save-dev
 */
var gulp = require('gulp'), //基础库
	imagemin = require('gulp-imagemin'), //图片压缩
	sass = require('gulp-ruby-sass'), //sass
	minifycss = require('gulp-minify-css'), //css压缩
	minhtml = require('gulp-htmlmin'),
	//jshint = require('gulp-jshint'), //js检查
	uglify = require('gulp-uglify'), //js压缩
	rename = require('gulp-rename'), //重命名
	concat = require('gulp-concat'), //合并文件
	clean = require('gulp-clean'), //清空文件夹
	tinylr = require('tiny-lr'), //livereload
	server = tinylr(),
	port = 35729,
	livereload = require('gulp-livereload'); //livereload
// HTML处理
gulp.task('html', function() {
	var htmlSrc = '*.html',
		htmlDst = 'dist';
	var options = {
		removeComments: true, //清除HTML注释
		collapseWhitespace: true, //压缩HTML
		collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
		removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
		removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
		removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
		minifyJS: true, //压缩页面JS
		minifyCSS: true //压缩页面CSS
	};
	gulp.src(htmlSrc)
		.pipe(livereload(server))
		.pipe(minhtml(options))
		.pipe(gulp.dest(htmlDst))
});
// 样式处理
gulp.task('css', function() {
	var cssSrc = [
			'css/reset.css',
			'css/index.css',
			'css/slide.css',
		],
		cssDst = 'css',
		cssDst2 = 'dist/css';

	gulp.src(cssSrc)
		//.pipe(sass({ style: 'expanded' }))
		.pipe(concat('main.css'))
		.pipe(gulp.dest(cssDst))
		.pipe(rename({ suffix: '.min' }))
		.pipe(minifycss())
		.pipe(gulp.dest(cssDst))
		.pipe(gulp.dest(cssDst2));
});
// 图片处理
gulp.task('images', function() {
	var imgSrc = 'img/**/*',
		imgDst = 'dist/img'; 
	gulp.src(imgSrc)
		.pipe(imagemin())
		.pipe(gulp.dest(imgDst));
})
// js处理
gulp.task('js', function() {
	var jsSrc = [
	'js/canvas.js',
	'js/index.js',
	],
	jsDst = 'js',
	jsDst2 = 'dist/js';

	gulp.src(jsSrc)
		//.pipe(jshint('.jshintrc'))
		//.pipe(jshint.reporter('default'))
		.pipe(concat('main.js'))
		.pipe(gulp.dest(jsDst))
		.pipe(rename({ suffix: '.min' }))
		.pipe(uglify())
		.pipe(livereload(server))
		.pipe(gulp.dest(jsDst))
		.pipe(gulp.dest(jsDst2));
});
// js处理
gulp.task('js1', function() {
	var jsSrc = ['js/jquery.DB_tabMotionBanner.min.js', 'js/jquery-1.8.3.min.js'],
		jsDst = 'dist/js';
	gulp.src(jsSrc)
		.pipe(gulp.dest(jsDst));
});
// 清空图片、样式、js
gulp.task('clean', function() {
	gulp.src(['dist/css', 'dist/js', 'dist/images'], { read: false })
		.pipe(clean());
});

// 默认任务 清空图片、样式、js并重建 运行语句 gulp
gulp.task('default', ['clean'], function() {
	gulp.start('html', 'css', 'images', 'js');
});

// 监听任务 运行语句 gulp watch
gulp.task('watch', function() {

	server.listen(port, function(err) {
		if(err) {
			return console.log(err);
		}

		// 监听html
		gulp.watch('*.html', function() {
			gulp.run('html');
		})

		// 监听css
		gulp.watch('css/*.css', function() {
			gulp.run('css');
		});
		// 监听css
		gulp.watch('scss/*.scss', function() {
			gulp.run('css');
		});
		// 监听images
		gulp.watch('img/**/*', function() {
			gulp.run('images');
		});

		// 监听js
		gulp.watch('js/*.js', function() {
			gulp.run('js');
		});
		// 监听js
		gulp.watch('js/*.js', function() {
			gulp.run('js1'); 
		});
	});
});